/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.BusinessLogic;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;

/**
 *
 * @author WEN
 */
public class Validation {

    private boolean DatePromo;
    private static String MonthFormat;
    private static String DayFormat;
    private static String YearFormat;
    private HashMap<String, String> lastDays;
    boolean DateReport;

    public Validation() {
        DatePromo = false;
        MonthFormat = "MMM";
        DayFormat = "dd";
        YearFormat = "yyyy";
        lastDays = new HashMap<String, String>();
        lastDays.put("01", "31");
        lastDays.put("02", "28");
        lastDays.put("03", "31");
        lastDays.put("04", "30");
        lastDays.put("05", "31");
        lastDays.put("06", "30");
        lastDays.put("07", "31");
        lastDays.put("08", "31");
        lastDays.put("09", "30");
        lastDays.put("10", "31");
        lastDays.put("11", "30");
        lastDays.put("12", "31");
        DateReport = false;
    }

    public boolean ValidateFloat(String num) {
    //    System.out.println("float");
        double number = -1;
        boolean ok = false;
        try {
            number = Double.parseDouble(num);
        } catch (NumberFormatException numberFormatException) {
            ok = false;
        }
        if (number >= 0.00) {
            ok = true;
        } else {
            ok = false;
        }
 //       System.out.println("ok" + ok);
        return ok;
    }

    public boolean ValidateInteger(String num) {
        boolean ok = false;
        int number = -1;
        try {
            number = Integer.parseInt(num);
        } catch (NumberFormatException numberFormatException) {
            ok = false;
        }
        if ((number >= 0)) {
            ok = true;
        } else {
            ok = false;
        }
        return ok;
    }

    public boolean ValidatePostal(String postalCode, String prinv) {
        if (postalCode.length() != 6) {
            return false;
        }
        char[] postal = postalCode.toUpperCase().toCharArray();

        int ascii1 = postalCode.charAt(1);
        int ascii2 = postalCode.toUpperCase().charAt(2);
        int ascii3 = postalCode.charAt(3);
        int ascii4 = postalCode.toUpperCase().charAt(4);
        int ascii5 = postalCode.charAt(5);

        if (ascii1 < 48 || ascii1 > 57) { //Is Number
            return false;
        } else if (ascii2 < 65 || ascii2 > 90) {
            return false;
        } else if (ascii3 < 48 || ascii3 > 57) {
            return false;
        } else if (ascii5 < 48 || ascii5 > 57) {
            return false;
        } else if (ascii4 < 65 || ascii4 > 90) {
            return false;
        } else {
            if (postal[0] == 'A' || postal[0] == 'B' || postal[0] == 'C' || postal[0]
                    == 'E' || postal[0] == 'G' || postal[0] == 'H' || postal[0]
                    == 'J' || postal[0] == 'K' || postal[0] == 'L' || postal[0]
                    == 'M' || postal[0] == 'N' || postal[0] == 'P' || postal[0]
                    == 'R' || postal[0] == 'S' || postal[0] == 'T' || postal[0]
                    == 'V' || postal[0] == 'X' || postal[0] == 'Y') {
                if (postal[0] == 'A' && prinv.equals("Newfoundland and Labrador")
                        == false) {
                    return false;
                } else if (postal[0] == 'B' && prinv.equals("Nova Scotia")
                        == false) {
                    return false;
                } else if (postal[0] == 'C' && prinv.equals(
                        "Prince Edward Island") == false) {
                    return false;
                } else if (postal[0] == 'E' && prinv.equals("New Brunswick")
                        == false) {
                    return false;
                } else if (postal[0] == 'G' && prinv.equals("Quebec")
                        == false) {
                    return false;
                } else if (postal[0] == 'H' && prinv.equals("Quebec")
                        == false) {
                    return false;
                } else if (postal[0] == 'J' && prinv.equals("Quebec")
                        == false) {
                    return false;
                } else if (postal[0] == 'K' && prinv.equals("Ontario")
                        == false) {
                    return false;
                } else if (postal[0] == 'L' && prinv.equals("Ontario")
                        == false) {
                    return false;
                } else if (postal[0] == 'M' && prinv.equals("Ontario")
                        == false) {
                    return false;
                } else if (postal[0] == 'N' && prinv.equals("Ontario")
                        == false) {
                    return false;
                } else if (postal[0] == 'P' && prinv.equals("Ontario")
                        == false) {
                    return false;
                } else if (postal[0] == 'R' && prinv.equals("Manitoba")
                        == false) {
                    return false;
                } else if (postal[0] == 'S' && prinv.equals("Saskatchewan")
                        == false) {
                    return false;
                } else if (postal[0] == 'T' && prinv.equals("Alberta")
                        == false) {
                    return false;
                } else if (postal[0] == 'V' && prinv.equals(
                        "British Columbia") == false) {
                    return false;
                } else if (postal[0] == 'X' && prinv.equals(
                        "Northwest Territories") == false) {
                    return false;
                } else if (postal[0] == 'X' && prinv.equals("Nunavut")
                        == false) {
                    return false;
                } else if (postal[0] == 'Y' && prinv.equals("Yukon")
                        == false) {
                    return false;
                }
            } else {
                return true;
            }
            return false;
        }
    }

    public boolean ValidateStartDate(int sm, int sd, int sy, int em, int ed, int ey) {
        //Validate Start Date
        if (sm == 1 || sm == 3 || sm == 5 || sm == 7 || sm == 8 || sm == 10 || sm
                == 12) {
            if (sd > 31) {
                DatePromo = false;
            } else {
                DatePromo = true;
                ValidateEndDate(sm, sd, sy, em, ed, ey);
            }
        } else if (sm == 4 || sm == 6 || sm == 9 || sm == 11) {
            if (sd > 30) {
                DatePromo = false;
            } else {
                DatePromo = true;
                ValidateEndDate(sm, sd, sy, em, ed, ey);
            }
        } else if (sm == 2) {
            if (sd > 29) {
                DatePromo = false;
            } else if (sd == 29) {
                if ((sy == 2012) || (sy == 2016) || (sy == 2020)) {
                    DatePromo = true;
                    ValidateEndDate(sm, sd, sy, em, ed, ey);
                } else {
                    DatePromo = false;
                }
            } else {
                DatePromo = true;
                ValidateEndDate(sm, sd, sy, em, ed, ey);
            }
        }
        return DatePromo;
    }

    private void ValidateEndDate(int sm, int sd, int sy, int em, int ed, int ey) {
        if (em == 1 || em == 3 || em == 5 || em == 7 || em == 8 || em == 10 || em
                == 12) {
            if (ed > 31) {
                DatePromo = false;
            } else {
                DatePromo = true;
                ValidateBoth(sm, sd, sy, em, ed, ey);
            }
        } else if (em == 4 || em == 6 || em == 9 || em == 11) {
            if (ed > 30) {
                DatePromo = false;
            } else {
                DatePromo = true;
                ValidateBoth(sm, sd, sy, em, ed, ey);
            }
        } else if (em == 2) {
            if (ed > 29) {
                DatePromo = false;
            } else if (ed == 29) {
                if ((ey == 2012) || (ey == 2016) || (ey == 2020)) {
                    DatePromo = true;
                    ValidateBoth(sm, sd, sy, em, ed, ey);
                } else {
                    DatePromo = false;
                }
            } else {
                DatePromo = true;
                ValidateBoth(sm, sd, sy, em, ed, ey);
            }
        }
    }

    private void ValidateBoth(int sm, int sd, int sy, int em, int ed, int ey) {

        //Validate one against each other
        if ((sm == em) && (sd == ed) && (sy == ey)) {
            DatePromo = true;
            ValidateActualDate(sm, sd, sy);
        } else if ((sm == em) && (ey == sy)) {
            if (sd > ed) {
                DatePromo = false;
            }
        } else if (ey == sy) {
            if (sm > em) {
                DatePromo = false;
            } else {
                DatePromo = true;
                ValidateActualDate(sm, sd, sy);
            }
        } else if (sy > ey) {
            DatePromo = false;
        } else {
            DatePromo = true;
            ValidateActualDate(sm, sd, sy);
        }
    }

    private void ValidateActualDate(int sm, int sd, int sy) {
        Calendar cal = Calendar.getInstance(); //Read time
        //Store hours, minutes and seconds
        SimpleDateFormat sdf1 = new SimpleDateFormat(MonthFormat);
        SimpleDateFormat sdf2 = new SimpleDateFormat(DayFormat);
        SimpleDateFormat sdf3 = new SimpleDateFormat(YearFormat);
        int month = 1, day, year;
        String smonth;

        //Turn into integers
        smonth = sdf1.format(cal.getTime());
        day = Integer.parseInt(sdf2.format(cal.getTime()));
        year = Integer.parseInt(sdf3.format(cal.getTime()));
   //     System.out.println(smonth + "" + day + "" + year);

        if (smonth.equals("Jan") || (smonth.equals("一月"))) {
            month = 1;
        } else if (smonth.equals("Feb") || (smonth.equals("二月"))) {
            month = 2;
        } else if (smonth.equals("Mar") || (smonth.equals("三月"))) {
            month = 3;
        } else if (smonth.equals("Apr") || (smonth.equals("四月"))) {
            month = 4;
        } else if (smonth.equals("May") || (smonth.equals("五月"))) {
            month = 5;
        } else if (smonth.equals("Jun") || (smonth.equals("六月"))) {
            month = 6;
        } else if (smonth.equals("Jul") || (smonth.equals("七月"))) {
            month = 7;
        } else if (smonth.equals("Aug") || (smonth.equals("八月"))) {
            month = 8;
        } else if (smonth.equals("Sep") || (smonth.equals("九月"))) {
            month = 9;
        } else if (smonth.equals("Oct") || (smonth.equals("十月"))) {
            month = 10;
        } else if (smonth.equals("Nov") || (smonth.equals("十一月"))) {
            month = 11;
        } else if (smonth.equals("Dec") || (smonth.equals("十二月"))) {
            month = 12;
        }

        if ((month == sm) && (day == sd) && (year == sy)) {
            DatePromo = true;
        } else if ((month == sm) && (year == sy)) {
            if (day > sd) {
                DatePromo = false;
            } else {
                DatePromo = true;
            }
        } else if (year == sy) {
            if (month > sm) {
                DatePromo = false;
            } else {
                DatePromo = true;
            }
        } else {
            if (year > sy) {
                DatePromo = false;
            } else {
                DatePromo = true;
            }
        }
    }

    public int validReportDate(String startReportDate, String endReportDate) {
        String splitStartDate[] = new String[3];
        String splitEndDate[] = new String[3];
        splitStartDate = startReportDate.split("-");
        splitEndDate = startReportDate.split("-");
        if (!splitStartDate[0].equals(splitEndDate[0])) {
      //      System.out.println("yyyyyyyyyy" + 1);
            return 1;
        } else if (splitStartDate[0].equals(splitEndDate[0]) && (!splitStartDate[1].equals(splitEndDate[1]))) {
     //      System.out.println("y" + 2);
            return 2;
        } else if (splitStartDate[0].equals(splitEndDate[0]) && splitStartDate[1].equals(splitEndDate[1])) {
     //       System.out.println("y" + 3);
            return 3;
        } else {
     //       System.out.println("y" + 0);
            return 0;
        }
    }

    public String getMonthReport(String endReportDate) {
        String splitEndDate[] = new String[3];
        splitEndDate = endReportDate.split("-");
        return splitEndDate[0] + "-" + splitEndDate[1] + "-" + lastDays.get(splitEndDate[1]);
    }

    public boolean validateReportDate(int sd, int sm, int sy, int ed, int em, int ey) {
        //Validate Start Date
        if (sm == 1 || sm == 3 || sm == 5 || sm == 7 || sm == 8 || sm == 10 || sm == 12) {
            if (sd > 31) {
                DateReport = false;
            } else {
                DateReport = true;
                ValidateEndDatee(sm, sd, sy, em, ed, ey);
            }
        } else if (sm == 4 || sm == 6 || sm == 9 || sm == 11) {
            if (sd > 30) {
                DateReport = false;
            } else {
                DateReport = true;
                ValidateEndDatee(sm, sd, sy, em, ed, ey);
            }
        } else if (sm == 2) {
            if (sd > 29) {
                DateReport = false;
            } else if (sd == 29) {
                if ((sy == 2012) || (sy == 2016) || (sy == 2020)) {
                    DateReport = true;
                    ValidateEndDatee(sm, sd, sy, em, ed, ey);
                } else {
                    DateReport = false;
                }
            } else {
                DateReport = true;
                ValidateEndDatee(sm, sd, sy, em, ed, ey);
            }
        }
        return DateReport;
    }

    private void ValidateEndDatee(int sm, int sd, int sy, int em, int ed, int ey) {
        if (em == 1 || em == 3 || em == 5 || em == 7 || em == 8 || em == 10 || em == 12) {
            if (ed > 31) {
                DateReport = false;
            } else {
                DateReport = true;
                ValidateBothh(sm, sd, sy, em, ed, ey);
            }
        } else if (em == 4 || em == 6 || em == 9 || em == 11) {
            if (ed > 30) {
                DateReport = false;
            } else {
                DateReport = true;
                ValidateBothh(sm, sd, sy, em, ed, ey);
            }
        } else if (em == 2) {
            if (ed > 29) {
                DateReport = false;
            } else if (ed == 29) {
                if ((ey == 2012) || (ey == 2016) || (ey == 2020)) {
                    DateReport = true;
                    ValidateBothh(sm, sd, sy, em, ed, ey);
                } else {
                    DateReport = false;
                }
            } else {
                DateReport = true;
                ValidateBothh(sm, sd, sy, em, ed, ey);
            }
        }
    }

    private void ValidateBothh(int sm, int sd, int sy, int em, int ed, int ey) {

        //Validate one against each other
        if ((sm == em) && (sd == ed) && (sy == ey)) {
            DateReport = true;
            ValidateActualDatee(em, ed, ey);
        } else if ((sm == em) && (ey == sy)) {
            if (sd > ed) {
                DateReport = false;
            }
        } else if (ey == sy) {
            if (sm > em) {
                DateReport = false;
            } else {
                DateReport = true;
                ValidateActualDatee(em, ed, ey);
            }
        } else if (sy > ey) {
            DateReport = false;
        } else {
            DateReport = true;
            ValidateActualDatee(em, ed, ey);
        }
    }

    private void ValidateActualDatee(int em, int ed, int ey) {
        Calendar cal = Calendar.getInstance(); //Read time
        //Store hours, minutes and seconds
        SimpleDateFormat sdf1 = new SimpleDateFormat(MonthFormat);
        SimpleDateFormat sdf2 = new SimpleDateFormat(DayFormat);
        SimpleDateFormat sdf3 = new SimpleDateFormat(YearFormat);
        int month = 1, day, year;
        String smonth;

        //Turn into integers
        smonth = sdf1.format(cal.getTime());
        day = Integer.parseInt(sdf2.format(cal.getTime()));
        year = Integer.parseInt(sdf3.format(cal.getTime()));
    //    System.out.println(smonth + "" + day + "" + year);

        if (smonth.equals("Jan") || (smonth.equals("一月"))) {
            month = 1;
        } else if (smonth.equals("Feb") || (smonth.equals("二月"))) {
            month = 2;
        } else if (smonth.equals("Mar") || (smonth.equals("三月"))) {
            month = 3;
        } else if (smonth.equals("Apr") || (smonth.equals("四月"))) {
            month = 4;
        } else if (smonth.equals("May") || (smonth.equals("五月"))) {
            month = 5;
        } else if (smonth.equals("Jun") || (smonth.equals("六月"))) {
            month = 6;
        } else if (smonth.equals("Jul") || (smonth.equals("七月"))) {
            month = 7;
        } else if (smonth.equals("Aug") || (smonth.equals("八月"))) {
            month = 8;
        } else if (smonth.equals("Sep") || (smonth.equals("九月"))) {
            month = 9;
        } else if (smonth.equals("Oct") || (smonth.equals("十月"))) {
            month = 10;
        } else if (smonth.equals("Nov") || (smonth.equals("十一月"))) {
            month = 11;
        } else if (smonth.equals("Dec") || (smonth.equals("十二月"))) {
            month = 12;
        }

        //Validate one against each other
        if ((month == em) && (day == ed) && (year == ey)) {
            DateReport = true;
        } else if ((month == em) && (ey == year)) {
            if (day < ed) {
                DateReport = false;
            }
        } else if (ey == year) {
            if (month < em) {
                DateReport = false;
            }
        } else if (year < ey) {
            DateReport = false;
        } else {
            DateReport = true;
        }
    }
}
